package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.UcenterMember;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-01-28
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {
    private static final Logger logger = LoggerFactory.getLogger(EduCommentController.class);
    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient client;
    /**
     * 评论分页查询
     * @param page
     * @param limit
     * @param courseId
     * @return
     */
    @GetMapping("commentsById/{page}/{limit}")
    public R commentsById(@PathVariable Long page, @PathVariable Long limit, String courseId){
        Page<EduComment> pageComment=new Page<>(page,limit);
        Map<String,Object> map=commentService.commentsById(pageComment,courseId);
        return R.ok().data(map);
    }

    /**
     * 添加评论
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(id)){
            throw new GuliException(20001,"请登录");
        }
        UcenterMember info = client.getInfo(id);
        logger.info("info为:{}",info);
        comment.setMemberId(id);
        comment.setAvatar(info.getAvatar());
        comment.setNickname(info.getNickname());
        commentService.save(comment);
        return R.ok();
    }
}

